package com.zzy.jdateutil.jdkdate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {

	public static void main(String[] args) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(format.format(new Date()));
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String date2Str(Date date, String pattern) {
		return DateUtil.date2Str(date, pattern, null);
	}

	public static String date2Str(Date date, String pattern, String timeZone) {
		if (date == null || pattern == null)
			return "";

		SimpleDateFormat format = new SimpleDateFormat(pattern);
		format.setTimeZone(TimeZone.getTimeZone("GMT-8:00"));
		if (timeZone != null)
			format.setTimeZone(TimeZone.getTimeZone("GMT" + timeZone + ":00"));
		return format.format(date);
	}

	/**
	 * 获取long类型时间戳
	 * 
	 * @param date
	 * @return
	 */
	public static long date2Timestamp(Date date) {
		if (date == null)
			return 0L;
		else
			return date.getTime() / 1000L;
	}

	public static Date timestamp2Date(long date) {
		if (date == 0L)
			return null;
		else
			return new Date(date * 1000L);
	}

}
